package LeetCodeHot100TopInterview;

public class Q014_longestCommonPrefix {

    //最长公共前缀
    //朴实无华的两两比较
    //O(所有字符个数)
    public String longestCommonPrefix(String[] strs) {
        if (strs == null || strs.length == 0){
            return "";
        }

        StringBuilder ans = new StringBuilder();
        int maxLen = strs[0].length();
        for (int i = 1; i < strs.length ; i++) {
            //  i  <->  i - 1 进行比较最长前缀
            int idx = 0;
            while (idx < strs[i].length() && idx < strs[i - 1].length() &&
                strs[i].charAt(idx) == strs[i - 1].charAt(idx)
            ){
                idx ++;
            }
            maxLen = Math.min(maxLen, idx);
        }

        for (int i = 0; i < maxLen; i++) {
            ans.append(strs[0].charAt(i));
        }
        return ans.toString();
    }

    public static void main(String[] args) {
        // 一遍过了
    }
}
